package Main;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author
 */
public class KetNoiCSDL {

    //Duong dan den server co so du lieu
    static String url = "jdbc:mysql://localhost:3306/";
    //Ten csdl
    static String dbName = "tracnghiem_db";
    //Driver su dung de ket noi csdl
    static String driver = "com.mysql.jdbc.Driver";
    //Username dang nhap server
    static String userName = "root";
    //Password
    static String password = "";
    //Bien tao ket noi
    private static Connection connection;
    //Chuoi ket noi csdl
    private static String connectionString = null;
    //Bien khoi tao cac thuoc tinh ket noi den csdl
    private static Properties pro;

    // ham doc file config.xml va thiet lap cac tham so ket noi
    public static void config() {

        pro = new Properties();
        connectionString = url + dbName;

        pro.setProperty("characterEncoding", "utf8");

        pro.setProperty("user", userName);


        pro.setProperty("password", password);
    }

    // mo ket noi den CSDL
    public static Connection open() throws SQLException {
        Driver driver = new org.gjt.mm.mysql.Driver();
        DriverManager.registerDriver(driver);
        config();
        connection = (Connection) DriverManager.getConnection(connectionString, pro);
        return connection;
    }

    // dong ket noi CSDL
    public static void close() {
        try {
            connection.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
   
}
